﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CFSystem.JXC.BasisInfo
{
    using CFSystem.JXC.Model;
    using CFSystem.JXC.BLL;

    public partial class FrmProductInfo : Form
    {
        private ProductInfo _product;
        private ProductBLL _productBLL;

        public delegate void UpdateDataBinding();
        public event UpdateDataBinding UpdateDataGridView;

        private bool _isDataValid = true;
        private bool _isadded = false;

        public FrmProductInfo()
        {
            InitializeComponent();

            AddEvent();

            _product = new ProductInfo();
            _productBLL = new ProductBLL();

            LoadAllCategories();
            LoadAllSpecifications();
            LoadAllColors();
            LoadAllBrands();
            LoadAllUnits();

            btnApply.Enabled = false;

            _isadded = true;
        }

        public FrmProductInfo(string productID) : this()
        {
            _product = _productBLL.GetProductByID(productID);

            GenerateFormData();

            _isadded = false;
        }

        private void GenerateFormData()
        {
            txtProductID.Text = Common.GetStringValue(_product.ProductID);
            txtProductName.Text = Common.GetStringValue(_product.ProductName);
            txtBarCode.Text = Common.GetStringValue(_product.BarCode);
            cboProductCategory.SelectedValue = _product.CatergoryID;
            cboProductCategory.Text = Common.GetStringValue(_product.CatergoryName);
            cboSpecification.SelectedValue = Common.GetStringValue(_product.SpecificationID);
            cboSpecification.Text = Common.GetStringValue(_product.SpecificationName);
            cboColor.SelectedValue = Common.GetStringValue(_product.ColorID);
            cboColor.Text = Common.GetStringValue(_product.ColorName);
            cboBrand.SelectedValue = Common.GetStringValue(_product.BrandID);
            cboBrand.Text = Common.GetStringValue(_product.BrandName);
            cboUnit.SelectedValue = Common.GetStringValue(_product.UnitID);
            cboUnit.Text = Common.GetStringValue(_product.UnitName);
            txtPurchase.Text = Common.GetStringValue(_product.PurchasePrice);
            txtPrice.Text = Common.GetStringValue(_product.SalePrice);
            txtStorageAmount.Text = Common.GetStringValue(_product.StoreNumber);
            txtStoragePrice.Text = Common.GetStringValue(_product.StorePrice);
            rtbNote.Text = Common.GetStringValue(_product.Note);
        }

        private void AddEvent()
        {
            foreach (Control control in this.grpProductInfo.Controls)
            {
                if (control is TextBox || control is ComboBox || control is RichTextBox)
                {
                    control.TextChanged += new EventHandler(control_TextChanged);
                }
            }
        }

        void control_TextChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void LoadAllUnits()
        {
            Unit unitBLL = new Unit();
            IList<UnitInfo> units = unitBLL.GetUnits();
            cboUnit.DataSource = units;
            cboUnit.DisplayMember = "UnitName";
            cboUnit.ValueMember = "UnitID";
            cboUnit.SelectedIndex = -1;
        }

        private void LoadAllBrands()
        {
            Brand brandBLL = new Brand();
            IList<BrandInfo> brands = brandBLL.GetBrands();
            cboBrand.DataSource = brands;
            cboBrand.DisplayMember = "BrandName";
            cboBrand.ValueMember = "BrandID";
            cboBrand.SelectedIndex = -1;
        }

        private void LoadAllColors()
        {
            Color1 colorBLL = new Color1();
            IList<ColorInfo> colors = colorBLL.GetColors();
            cboColor.DataSource = colors;
            cboColor.DisplayMember = "ColorName";
            cboColor.ValueMember = "ColorID";
            cboColor.SelectedIndex = -1;
        }

        private void LoadAllSpecifications()
        {
            Size1 size = new Size1();
            IList<SizeInfo> sizes = size.GetSizes();
            cboSpecification.DataSource = sizes;
            cboSpecification.DisplayMember = "SizeName";
            cboSpecification.ValueMember = "SizeID";
            cboSpecification.SelectedIndex = -1;
        }

        private void LoadAllCategories()
        {
            ProdCatergory categoryBLL = new ProdCatergory();
            IList<ProdCatergoryInfo> categories = categoryBLL.GetProdCatergorys();
            cboProductCategory.DataSource = categories;
            cboProductCategory.DisplayMember = "ProdCatergoryName";
            cboProductCategory.ValueMember = "ProdCatergoryID";
            cboProductCategory.SelectedIndex = -1;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (btnApply.Enabled)
            {
                GenerateProductData();
                try
                {
                    if (_isadded)
                    {
                        _productBLL.Insert(_product);
                        MessageBox.Show("添加数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        _productBLL.Update(_product);
                        MessageBox.Show("更新数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }                    
                    UpdateDataGridView();
                    this.Close();
                }
                catch (Exception ex)
                {
                    if (_isadded)
                    {
                        MessageBox.Show("添加数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show("更新数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            _product = null;
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            GenerateProductData();
            if (_isDataValid)
            {
                try
                {
                    if (_isadded)
                    {
                        _productBLL.Insert(_product);
                        MessageBox.Show("添加数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        _productBLL.Update(_product);
                        MessageBox.Show("更新数据成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    btnApply.Enabled = false;
                    UpdateDataGridView();
                }
                catch (Exception ex)
                {
                    if (_isadded)
                    {
                        MessageBox.Show("添加数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show("更新数据失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void GenerateProductData()
        {
            try
            {
                _product.ProductID = Common.CheckIsEmpty(lblProductID.Text, txtProductID.Text);
                _product.ProductName = Common.CheckIsEmpty(lblProductName.Text, txtProductName.Text);
                _product.BarCode = Common.CheckIsEmpty(lblBarCode.Text, txtBarCode.Text);
                _product.CatergoryID = Common.GetIntegerValue(cboProductCategory.SelectedValue);
                _product.CatergoryName = Common.GetStringValue(cboProductCategory.Text.Trim());
                _product.SpecificationID = Common.GetIntegerValue(cboSpecification.SelectedValue);
                _product.SpecificationName = Common.GetStringValue(cboSpecification.Text.Trim());
                _product.ColorID = Common.GetIntegerValue(cboColor.SelectedValue);
                _product.ColorName = Common.GetStringValue(cboColor.Text.Trim());
                _product.BrandID = Common.GetIntegerValue(cboBrand.SelectedValue);
                _product.BrandName = Common.GetStringValue(cboBrand.Text.Trim());
                _product.UnitID = Common.GetIntegerValue(cboUnit.SelectedValue);
                _product.UnitName = Common.GetStringValue(cboUnit.Text.Trim());
                _product.PurchasePrice = Common.GetStringValue(txtPurchase.Text);
                _product.SalePrice = Common.GetStringValue(txtPrice.Text);
                _product.StoreNumber = Common.CheckNumber(lblStorageAmount.Text, txtStorageAmount.Text);
                _product.StorePrice = Common.GetStringValue(txtStoragePrice.Text);
                _product.Note = Common.GetStringValue(rtbNote.Text);
            }
            catch (Exception ex)
            {
                _isDataValid = false;
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            FrmProductCatergory formProdCtgy = new FrmProductCatergory();
            formProdCtgy.StartPosition = FormStartPosition.CenterParent;
            formProdCtgy.ShowDialog();
        }

        private void btnAddSpecification_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Size);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }

        private void btnAddColor_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Color);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }

        private void btnAddBrand_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Brand);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }

        private void btnAddMeasure_Click(object sender, EventArgs e)
        {
            FrmBaseSetting form = new FrmBaseSetting(FrmBaseSetting.BaseSetting.Unit);
            form.StartPosition = FormStartPosition.CenterParent;
            form.ShowDialog();
        }

        private void FrmProductInfo_Load(object sender, EventArgs e)
        {

        }
    }
}
